import 'dart:convert';
import 'dart:core';

import 'package:dio/dio.dart';


const String BASEURL = "http://10.0.4.184:12345";

class HttpRequest {
  static BaseOptions baseOptions = BaseOptions(connectTimeout: 1000);
  static Dio dio = Dio(baseOptions);

  static Future request(
      String url, String method, Map<String, dynamic> params,Map<String, dynamic> data) async {
    Options options = Options();
    options.method = method;

    //添加拦截器
    dio.interceptors
        .add(InterceptorsWrapper(onRequest: (RequestOptions options) {
      options.headers['type'] = 1002; // 假渠道号
      return options;
    }, onResponse: (Response response) {
      Map<String, dynamic> res = jsonDecode(response.toString());
      if (res['code'] != 1) {
        print(res['msg']);
        throw res;
      }
      return res['data'];
    }, onError: (DioError e) {
      throw e;
    }));
    try {
      Response response = await dio.request(BASEURL + url,data: json.encode(data),
          queryParameters: params, options: options);
      return response;
    } on DioError catch (e) {
      throw e;
    }
  }

  static Future get(url, params) async {
    try {
      Response response = await request(url, 'get', params,{});
      return response;
    } on DioError catch (e) {
      throw e;
    }
  }

  static Future post(url, data) async {
    try {
      Response response = await request(url, 'post',data,{});
      return response;
    } on DioError catch (e) {
      throw e;
    }
  }
}
